﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;
using System.IO;
using UcAsp.Net.Comm;
using UcAsp.Net.Model;
namespace UcAsp.Net.BLL
{
     public  class QrCodeBLL
    {
         private   string path = ConfigurationManager.AppSettings["QR"];
         private   string basepath = AppDomain.CurrentDomain.BaseDirectory;

         public QrCodeBLL()         
         {
             if (path.Substring(path.Length - 1, 1) != "\\")
             {
                 path = path + "\\";
             }
             if (basepath.Substring(basepath.Length - 1, 1) != "\\")
             {
                 basepath = basepath + "\\";
             }
         }
         public string GetQrImagePath(string fileid)
         {
             if (File.Exists(basepath + path + fileid + ".jpg"))
             {
                 return path + fileid + ".jpg";
             }
             else
             { 
               return CreateQrImage(fileid);
             }
         }
         private  string CreateQrImage(string fileid)
         {
             int width =int.Parse(ConfigurationManager.AppSettings["QRWidth"]);
             int height =int.Parse(ConfigurationManager.AppSettings["QRHeight"]);
             CreateQr qr = new CreateQr(width, height);
             ArticlesBLL articlebll = new ArticlesBLL();
             Articles art = articlebll.GetArticles(int.Parse(fileid));
             CategoriesBLL categoresbll = new CategoriesBLL();
             Categories cate = categoresbll.GetCategoriesById(art.CategorieID);
             SiteSetting config = SettingConfig.GetSiteSetting;
             string code = config.SiteUrl + "/" + cate.ShowPath + "/";
             if (!string.IsNullOrEmpty(art.ShowPath))
             {
                 code = code + art.ShowPath + ".shtml";
             }
             else {
                 code = code + art.ID+".shtml";
             }
             qr.CreateQrFile(code, basepath + path, fileid);
             return path + fileid + ".jpg";

         }
         private bool DeleteQrImage(string fileid)
         {
             try
             {
                 if (File.Exists(basepath + path + fileid + ".jpg"))
                 {
                     File.Delete(basepath + path + fileid + ".jpg");
                 }
                 return true;
             }
             catch
             {
                 return false;
             }
         }
    }
}
